<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 技术教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="本文主要讲述 AOP 在 mall 项目中的应用，通过在 controller 层建一个切面来实现接口访问的统一日志记录。  #👍 相关视频教程SpringBoot应用中使用AOP记录接口访问日志open in new window #AOP AOP为Aspect Oriented Programming的缩写，意为：面向切面编程，通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot应用中使用AOP记录接口访问日志">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2024/01/cd1563ab1e3d.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="本文主要讲述 AOP 在 mall 项目中的应用，通过在 controller 层建一个切面来实现接口访问的统一日志记录。  #👍 相关视频教程SpringBoot应用中使用AOP记录接口访问日志open in new window #AOP AOP为Aspect Oriented Programming的缩写，意为：面向切面编程，通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2024-01-21T09:28:07.000Z">
<meta property="article:modified_time" content="2024-02-02T14:53:00.974Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="宅男,热血">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2024/01/cd1563ab1e3d.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 技术教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-02 22:53:00'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">SpringBoot应用中使用AOP记录接口访问日志</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-21T09:28:07.000Z" title="发表于 2024-01-21 17:28:07">2024-01-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-02T14:53:00.974Z" title="更新于 2024-02-02 22:53:00">2024-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/">技术教程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/">mall</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/%E6%8A%80%E6%9C%AF%E8%A6%81%E7%82%B9%E7%AF%87/">技术要点篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SpringBoot应用中使用AOP记录接口访问日志"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><blockquote>
<p>本文主要讲述 AOP 在 mall 项目中的应用，通过在 controller 层建一个切面来实现接口访问的统一日志记录。</p>
</blockquote>
<h2 id="👍-相关视频教程"><a href="#👍-相关视频教程" class="headerlink" title="#👍 相关视频教程"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%F0%9F%91%8D-%E7%9B%B8%E5%85%B3%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B">#</a>👍 相关视频教程</h2><p><a target="_blank" rel="noopener" href="https://t.zsxq.com/0e7M5lodv">SpringBoot应用中使用AOP记录接口访问日志open in new window</a></p>
<h2 id="AOP"><a href="#AOP" class="headerlink" title="#AOP"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#aop">#</a>AOP</h2><blockquote>
<p>AOP为Aspect Oriented Programming的缩写，意为：面向切面编程，通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。利用AOP可以对业务逻辑的各个部分进行隔离，从而使得业务逻辑各部分之间的耦合度降低，提高程序的可重用性，同时提高了开发的效率。</p>
</blockquote>
<h3 id="AOP的相关术语"><a href="#AOP的相关术语" class="headerlink" title="#AOP的相关术语"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#aop%E7%9A%84%E7%9B%B8%E5%85%B3%E6%9C%AF%E8%AF%AD">#</a>AOP的相关术语</h3><h4 id="通知（Advice）"><a href="#通知（Advice）" class="headerlink" title="#通知（Advice）"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E9%80%9A%E7%9F%A5-advice">#</a>通知（Advice）</h4><p>通知描述了切面要完成的工作以及何时执行。比如我们的日志切面需要记录每个接口调用时长，就需要在接口调用前后分别记录当前时间，再取差值。</p>
<ul>
<li>前置通知（Before）：在目标方法调用前调用通知功能；</li>
<li>后置通知（After）：在目标方法调用之后调用通知功能，不关心方法的返回结果；</li>
<li>返回通知（AfterReturning）：在目标方法成功执行之后调用通知功能；</li>
<li>异常通知（AfterThrowing）：在目标方法抛出异常后调用通知功能；</li>
<li>环绕通知（Around）：通知包裹了目标方法，在目标方法调用之前和之后执行自定义的行为。</li>
</ul>
<h4 id="连接点（JoinPoint）"><a href="#连接点（JoinPoint）" class="headerlink" title="#连接点（JoinPoint）"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E8%BF%9E%E6%8E%A5%E7%82%B9-joinpoint">#</a>连接点（JoinPoint）</h4><p>通知功能被应用的时机。比如接口方法被调用的时候就是日志切面的连接点。</p>
<h4 id="切点（Pointcut）"><a href="#切点（Pointcut）" class="headerlink" title="#切点（Pointcut）"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E5%88%87%E7%82%B9-pointcut">#</a>切点（Pointcut）</h4><p>切点定义了通知功能被应用的范围。比如日志切面的应用范围就是所有接口，即所有controller层的接口方法。</p>
<h4 id="切面（Aspect）"><a href="#切面（Aspect）" class="headerlink" title="#切面（Aspect）"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E5%88%87%E9%9D%A2-aspect">#</a>切面（Aspect）</h4><p>切面是通知和切点的结合，定义了何时、何地应用通知功能。</p>
<h4 id="引入（Introduction）"><a href="#引入（Introduction）" class="headerlink" title="#引入（Introduction）"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E5%BC%95%E5%85%A5-introduction">#</a>引入（Introduction）</h4><p>在无需修改现有类的情况下，向现有的类添加新方法或属性。</p>
<h4 id="织入（Weaving）"><a href="#织入（Weaving）" class="headerlink" title="#织入（Weaving）"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E7%BB%87%E5%85%A5-weaving">#</a>织入（Weaving）</h4><p>把切面应用到目标对象并创建新的代理对象的过程。</p>
<h2 id="Spring中使用注解创建切面"><a href="#Spring中使用注解创建切面" class="headerlink" title="#Spring中使用注解创建切面"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#spring%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E5%88%9B%E5%BB%BA%E5%88%87%E9%9D%A2">#</a>Spring中使用注解创建切面</h2><h3 id="相关注解"><a href="#相关注解" class="headerlink" title="#相关注解"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E7%9B%B8%E5%85%B3%E6%B3%A8%E8%A7%A3">#</a>相关注解</h3><ul>
<li>@Aspect：用于定义切面</li>
<li>@Before：通知方法会在目标方法调用之前执行</li>
<li>@After：通知方法会在目标方法返回或抛出异常后执行</li>
<li>@AfterReturning：通知方法会在目标方法返回后执行</li>
<li>@AfterThrowing：通知方法会在目标方法抛出异常后执行</li>
<li>@Around：通知方法会将目标方法封装起来</li>
<li>@Pointcut：定义切点表达式</li>
</ul>
<h3 id="切点表达式"><a href="#切点表达式" class="headerlink" title="#切点表达式"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E5%88%87%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F">#</a>切点表达式</h3><p>指定了通知被应用的范围，表达式格式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">execution(方法修饰符 返回类型 方法所属的包.类名.方法名称(方法参数)</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//com.macro.mall.tiny.controller包中所有类的public方法都应用切面里的通知</span><br><span class="line">execution(public * com.macro.mall.tiny.controller.*.*(..))</span><br><span class="line">//com.macro.mall.tiny.service包及其子包下所有类中的所有方法都应用切面里的通知</span><br><span class="line">execution(* com.macro.mall.tiny.service..*.*(..))</span><br><span class="line">//com.macro.mall.tiny.service.PmsBrandService类中的所有方法都应用切面里的通知</span><br><span class="line">execution(* com.macro.mall.tiny.service.PmsBrandService.*(..))</span><br></pre></td></tr></table></figure>

<h2 id="添加AOP切面实现接口日志记录"><a href="#添加AOP切面实现接口日志记录" class="headerlink" title="#添加AOP切面实现接口日志记录"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E6%B7%BB%E5%8A%A0aop%E5%88%87%E9%9D%A2%E5%AE%9E%E7%8E%B0%E6%8E%A5%E5%8F%A3%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95">#</a>添加AOP切面实现接口日志记录</h2><h3 id="添加日志信息封装类WebLog"><a href="#添加日志信息封装类WebLog" class="headerlink" title="#添加日志信息封装类WebLog"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E6%B7%BB%E5%8A%A0%E6%97%A5%E5%BF%97%E4%BF%A1%E6%81%AF%E5%B0%81%E8%A3%85%E7%B1%BBweblog">#</a>添加日志信息封装类WebLog</h3><blockquote>
<p>用于封装需要记录的日志信息，包括操作的描述、时间、消耗时间、url、请求参数和返回结果等信息。</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">package com.macro.mall.tiny.dto;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Controller层的日志封装类</span><br><span class="line"> * Created by macro on 2018/4/26.</span><br><span class="line"> */</span><br><span class="line">public class WebLog &#123;</span><br><span class="line">    /**</span><br><span class="line">     * 操作描述</span><br><span class="line">     */</span><br><span class="line">    private String description;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 操作用户</span><br><span class="line">     */</span><br><span class="line">    private String username;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 操作时间</span><br><span class="line">     */</span><br><span class="line">    private Long startTime;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 消耗时间</span><br><span class="line">     */</span><br><span class="line">    private Integer spendTime;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 根路径</span><br><span class="line">     */</span><br><span class="line">    private String basePath;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * URI</span><br><span class="line">     */</span><br><span class="line">    private String uri;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * URL</span><br><span class="line">     */</span><br><span class="line">    private String url;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 请求类型</span><br><span class="line">     */</span><br><span class="line">    private String method;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * IP地址</span><br><span class="line">     */</span><br><span class="line">    private String ip;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 请求参数</span><br><span class="line">     */</span><br><span class="line">    private Object parameter;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 请求返回的结果</span><br><span class="line">     */</span><br><span class="line">    private Object result;</span><br><span class="line"></span><br><span class="line">    //省略了getter,setter方法</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="添加切面类WebLogAspect"><a href="#添加切面类WebLogAspect" class="headerlink" title="#添加切面类WebLogAspect"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E6%B7%BB%E5%8A%A0%E5%88%87%E9%9D%A2%E7%B1%BBweblogaspect">#</a>添加切面类WebLogAspect</h3><blockquote>
<p>定义了一个日志切面，在环绕通知中获取日志需要的信息，并应用到controller层中所有的public方法中去。</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line">package com.macro.mall.tiny.component;</span><br><span class="line"></span><br><span class="line">import cn.hutool.core.util.StrUtil;</span><br><span class="line">import cn.hutool.core.util.URLUtil;</span><br><span class="line">import cn.hutool.json.JSONUtil;</span><br><span class="line">import com.macro.mall.tiny.dto.WebLog;</span><br><span class="line">import io.swagger.annotations.ApiOperation;</span><br><span class="line">import org.aspectj.lang.JoinPoint;</span><br><span class="line">import org.aspectj.lang.ProceedingJoinPoint;</span><br><span class="line">import org.aspectj.lang.Signature;</span><br><span class="line">import org.aspectj.lang.annotation.*;</span><br><span class="line">import org.aspectj.lang.reflect.MethodSignature;</span><br><span class="line">import org.slf4j.Logger;</span><br><span class="line">import org.slf4j.LoggerFactory;</span><br><span class="line">import org.springframework.core.annotation.Order;</span><br><span class="line">import org.springframework.stereotype.Component;</span><br><span class="line">import org.springframework.util.StringUtils;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestBody;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestParam;</span><br><span class="line">import org.springframework.web.context.request.RequestContextHolder;</span><br><span class="line">import org.springframework.web.context.request.ServletRequestAttributes;</span><br><span class="line"></span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import java.lang.reflect.Method;</span><br><span class="line">import java.lang.reflect.Parameter;</span><br><span class="line">import java.util.ArrayList;</span><br><span class="line">import java.util.HashMap;</span><br><span class="line">import java.util.List;</span><br><span class="line">import java.util.Map;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 统一日志处理切面</span><br><span class="line"> * Created by macro on 2018/4/26.</span><br><span class="line"> */</span><br><span class="line">@Aspect</span><br><span class="line">@Component</span><br><span class="line">@Order(1)</span><br><span class="line">public class WebLogAspect &#123;</span><br><span class="line">    private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class);</span><br><span class="line"></span><br><span class="line">    @Pointcut(&quot;execution(public * com.macro.mall.tiny.controller.*.*(..))&quot;)</span><br><span class="line">    public void webLog() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Before(&quot;webLog()&quot;)</span><br><span class="line">    public void doBefore(JoinPoint joinPoint) throws Throwable &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @AfterReturning(value = &quot;webLog()&quot;, returning = &quot;ret&quot;)</span><br><span class="line">    public void doAfterReturning(Object ret) throws Throwable &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Around(&quot;webLog()&quot;)</span><br><span class="line">    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable &#123;</span><br><span class="line">        long startTime = System.currentTimeMillis();</span><br><span class="line">        //获取当前请求对象</span><br><span class="line">        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();</span><br><span class="line">        HttpServletRequest request = attributes.getRequest();</span><br><span class="line">        //记录请求信息</span><br><span class="line">        WebLog webLog = new WebLog();</span><br><span class="line">        Object result = joinPoint.proceed();</span><br><span class="line">        Signature signature = joinPoint.getSignature();</span><br><span class="line">        MethodSignature methodSignature = (MethodSignature) signature;</span><br><span class="line">        Method method = methodSignature.getMethod();</span><br><span class="line">        if (method.isAnnotationPresent(ApiOperation.class)) &#123;</span><br><span class="line">            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);</span><br><span class="line">            webLog.setDescription(apiOperation.value());</span><br><span class="line">        &#125;</span><br><span class="line">        long endTime = System.currentTimeMillis();</span><br><span class="line">        String urlStr = request.getRequestURL().toString();</span><br><span class="line">        webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));</span><br><span class="line">        webLog.setIp(request.getRemoteUser());</span><br><span class="line">        webLog.setMethod(request.getMethod());</span><br><span class="line">        webLog.setParameter(getParameter(method, joinPoint.getArgs()));</span><br><span class="line">        webLog.setResult(result);</span><br><span class="line">        webLog.setSpendTime((int) (endTime - startTime));</span><br><span class="line">        webLog.setStartTime(startTime);</span><br><span class="line">        webLog.setUri(request.getRequestURI());</span><br><span class="line">        webLog.setUrl(request.getRequestURL().toString());</span><br><span class="line">        LOGGER.info(&quot;&#123;&#125;&quot;, JSONUtil.parse(webLog));</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 根据方法和传入的参数获取请求参数</span><br><span class="line">     */</span><br><span class="line">    private Object getParameter(Method method, Object[] args) &#123;</span><br><span class="line">        List&lt;Object&gt; argList = new ArrayList&lt;&gt;();</span><br><span class="line">        Parameter[] parameters = method.getParameters();</span><br><span class="line">        for (int i = 0; i &lt; parameters.length; i++) &#123;</span><br><span class="line">            //将RequestBody注解修饰的参数作为请求参数</span><br><span class="line">            RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);</span><br><span class="line">            if (requestBody != null) &#123;</span><br><span class="line">                argList.add(args[i]);</span><br><span class="line">            &#125;</span><br><span class="line">            //将RequestParam注解修饰的参数作为请求参数</span><br><span class="line">            RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);</span><br><span class="line">            if (requestParam != null) &#123;</span><br><span class="line">                Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();</span><br><span class="line">                String key = parameters[i].getName();</span><br><span class="line">                if (!StringUtils.isEmpty(requestParam.value())) &#123;</span><br><span class="line">                    key = requestParam.value();</span><br><span class="line">                &#125;</span><br><span class="line">                map.put(key, args[i]);</span><br><span class="line">                argList.add(map);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        if (argList.size() == 0) &#123;</span><br><span class="line">            return null;</span><br><span class="line">        &#125; else if (argList.size() == 1) &#123;</span><br><span class="line">            return argList.get(0);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            return argList;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="进行接口测试"><a href="#进行接口测试" class="headerlink" title="#进行接口测试"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E8%BF%9B%E8%A1%8C%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95">#</a>进行接口测试</h2><p>运行项目并访问：<a target="_blank" rel="noopener" href="http://localhost:8080/swagger-ui.html">http://localhost:8080/swagger-ui.html</a></p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:57_refer_screen_107-e7f0aae3.png"></p>
<p>可以看到控制住台中会打印如下日志信息：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	&quot;result&quot;: &#123;</span><br><span class="line">		&quot;code&quot;: 200,</span><br><span class="line">		&quot;data&quot;: &#123;</span><br><span class="line">			&quot;total&quot;: 11,</span><br><span class="line">			&quot;totalPage&quot;: 11,</span><br><span class="line">			&quot;pageSize&quot;: 1,</span><br><span class="line">			&quot;list&quot;: [&#123;</span><br><span class="line">				&quot;productCommentCount&quot;: 100,</span><br><span class="line">				&quot;name&quot;: &quot;万和&quot;,</span><br><span class="line">				&quot;bigPic&quot;: &quot;&quot;,</span><br><span class="line">				&quot;logo&quot;: &quot;http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg&quot;,</span><br><span class="line">				&quot;showStatus&quot;: 1,</span><br><span class="line">				&quot;id&quot;: 1,</span><br><span class="line">				&quot;sort&quot;: 0,</span><br><span class="line">				&quot;productCount&quot;: 100,</span><br><span class="line">				&quot;firstLetter&quot;: &quot;W&quot;,</span><br><span class="line">				&quot;factoryStatus&quot;: 1</span><br><span class="line">			&#125;],</span><br><span class="line">			&quot;pageNum&quot;: 1</span><br><span class="line">		&#125;,</span><br><span class="line">		&quot;message&quot;: &quot;操作成功&quot;</span><br><span class="line">	&#125;,</span><br><span class="line">	&quot;basePath&quot;: &quot;http://localhost:8080&quot;,</span><br><span class="line">	&quot;method&quot;: &quot;GET&quot;,</span><br><span class="line">	&quot;parameter&quot;: [&#123;</span><br><span class="line">		&quot;pageNum&quot;: 1</span><br><span class="line">	&#125;, &#123;</span><br><span class="line">		&quot;pageSize&quot;: 1</span><br><span class="line">	&#125;],</span><br><span class="line">	&quot;description&quot;: &quot;分页查询品牌列表&quot;,</span><br><span class="line">	&quot;startTime&quot;: 1561273191861,</span><br><span class="line">	&quot;uri&quot;: &quot;/brand/list&quot;,</span><br><span class="line">	&quot;url&quot;: &quot;http://localhost:8080/brand/list&quot;,</span><br><span class="line">	&quot;spendTime&quot;: 101</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="项目源码地址"><a href="#项目源码地址" class="headerlink" title="#项目源码地址"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/aop_log.html#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80">#</a>项目源码地址</h2><p><a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-aop">https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-aop</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2024/01/cd1563ab1e3d.html">https://ko25891wan.gitlab.io/2024/01/cd1563ab1e3d.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/01/94dcc9008f8c.html" title="SpringBoot中处理校验逻辑的两种方式，真的很机智！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SpringBoot中处理校验逻辑的两种方式，真的很机智！</div></div></a></div><div class="next-post pull-right"><a href="/2024/01/b323e22f1028.html" title="RabbitMQ实现延迟消息居然如此简单，整个插件就完事了！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">RabbitMQ实现延迟消息居然如此简单，整个插件就完事了！</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%91%8D-%E7%9B%B8%E5%85%B3%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B"><span class="toc-number">1.</span> <span class="toc-text">👍 相关视频教程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#AOP"><span class="toc-number">2.</span> <span class="toc-text">AOP</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#AOP%E7%9A%84%E7%9B%B8%E5%85%B3%E6%9C%AF%E8%AF%AD"><span class="toc-number">2.1.</span> <span class="toc-text">AOP的相关术语</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E7%9F%A5%EF%BC%88Advice%EF%BC%89"><span class="toc-number">2.1.1.</span> <span class="toc-text">通知（Advice）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E7%82%B9%EF%BC%88JoinPoint%EF%BC%89"><span class="toc-number">2.1.2.</span> <span class="toc-text">连接点（JoinPoint）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%87%E7%82%B9%EF%BC%88Pointcut%EF%BC%89"><span class="toc-number">2.1.3.</span> <span class="toc-text">切点（Pointcut）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%87%E9%9D%A2%EF%BC%88Aspect%EF%BC%89"><span class="toc-number">2.1.4.</span> <span class="toc-text">切面（Aspect）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%95%E5%85%A5%EF%BC%88Introduction%EF%BC%89"><span class="toc-number">2.1.5.</span> <span class="toc-text">引入（Introduction）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%87%E5%85%A5%EF%BC%88Weaving%EF%BC%89"><span class="toc-number">2.1.6.</span> <span class="toc-text">织入（Weaving）</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E5%88%9B%E5%BB%BA%E5%88%87%E9%9D%A2"><span class="toc-number">3.</span> <span class="toc-text">Spring中使用注解创建切面</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E6%B3%A8%E8%A7%A3"><span class="toc-number">3.1.</span> <span class="toc-text">相关注解</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%87%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">3.2.</span> <span class="toc-text">切点表达式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0AOP%E5%88%87%E9%9D%A2%E5%AE%9E%E7%8E%B0%E6%8E%A5%E5%8F%A3%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95"><span class="toc-number">4.</span> <span class="toc-text">添加AOP切面实现接口日志记录</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E6%97%A5%E5%BF%97%E4%BF%A1%E6%81%AF%E5%B0%81%E8%A3%85%E7%B1%BBWebLog"><span class="toc-number">4.1.</span> <span class="toc-text">添加日志信息封装类WebLog</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E5%88%87%E9%9D%A2%E7%B1%BBWebLogAspect"><span class="toc-number">4.2.</span> <span class="toc-text">添加切面类WebLogAspect</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9B%E8%A1%8C%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95"><span class="toc-number">5.</span> <span class="toc-text">进行接口测试</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80"><span class="toc-number">6.</span> <span class="toc-text">项目源码地址</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>